from includes import * 
from State import *
from gameState import *
from loadState import *
from Gui.Gui import *
from MainMenuWidget import *

class menuState(State):
	
	def __init__(self):
			State.__init__(self)
			self.screen = pygame.display.get_surface()
			self.name = "menuState()"
	def init(self):
		self.needsInit = 0
		print "init() menuState"	
		self.gui = Gui()
		
		# Build the gui for the main menu.
		self.widget = MainMenuWidget(self)
		
	def update(self):
		# Get the mouse position TODO: maybe should be moved down to where its only called
		# if the mouse is in the event? could duplicate code?
		mpos = pygame.mouse.get_pos()
		for e in pygame.event.get():
			# the close button controlled by the window manager. TODO: this might need a change.
			# Cicking on the close button just kills the state not the app. The next like does the same
			# but uses the escape key as a trigger which is acceptable.
			if e.type is QUIT: self.run = 0  
			if e.type is KEYDOWN and e.key == K_ESCAPE: self.run = 0
			if e.type is MOUSEBUTTONUP: 
			
				# TODO: will need a change when getClick gets refactored.
				clicked = self.gui.getClick(mpos[0], mpos[1])
				# print clicked  #returns the GuiObject that was clicked.
				
				# After a click is detected, checks to see if there is an event on the queue (or multiple)
				# and fires it (them)
				if clicked:
					if clicked.onClick:
						for event in clicked.onClick:
							event(clicked, self.gui)
		# Run the update method on the Gui.
		self.gui.update()
	
	# The draw loop for this state. all drawing should be placed here, and the last statment should be a flip()
	def draw(self):
		self.gui.draw()
		pygame.display.flip()
	

